var DBHelper = this;
DBHelper.host = "https://xnsmb.xnsoft.net.cn/";
//DBHelper.host = "http://shshiapi.xnsoft.net.cn/";
//»ñÈ¡TOKEN
DBHelper.getToken = function (name, pwd, success, error) {
    if (localStorage.getItem("token") == "" || localStorage.getItem("token") == undefined || localStorage.getItem("token") == null) {
        $.ajax({
            url: DBHelper.host + "Token?AppId=manage",
            data: {
                grant_type: 'password',
                username: name,
                password: pwd
            },
            type: 'POST',
            beforeSend: function() {
                loadingImg()
            },
            success: function(data) {
                $('.loading').remove()
                localStorage.setItem("token", data.access_token);
                success(data)
            },
            error: function(data) {
                $('.loading').remove()
                error(data)
            }
        });
    } else {
        return localStorage.getItem("token")
    }
    return ""
}
//GETÇëÇó
DBHelper.Get = function(serverName, success, error) {
    $.ajax({
        url: DBHelper.host + 'api/' + serverName,
        beforeSend: function(request) {
            loadingImg()
            request.setRequestHeader("Authorization", 'bearer ' + localStorage.getItem("token"));
        },
        //data: { Ö÷Ìå²ÎÊý },
        type: 'GET',
        success: function(data) {
            $('.loading').remove()
            success(data)
        },
        error: function(data) {
            $('.loading').remove()
            if (data.status == "401") {
                alert('请重新登录')
                location.href = "/admin/login.html"
                //location.href='login.html'
            }
            error(data)
        }
    });
}

DBHelper.GetA = function(serverName, success, error) {
    $.ajax({
        url: DBHelper.host + 'api/' + serverName,
        beforeSend: function(request) {
            loadingImg()
            request.setRequestHeader("Authorization", 'bearer ' + localStorage.getItem("token"));
        },
        //data: { Ö÷Ìå²ÎÊý },
        type: 'GET',
        async:false,
        success: function(data) {
            $('.loading').remove()
            success(data)
        },
        error: function(data) {
            $('.loading').remove()
            if (data.status == "401") {
                alert('请重新登录')
                location.href = "/admin/login.html"
                //location.href='login.html'
            }
            error(data)
        }
    });
}
//POSTÇëÇó
DBHelper.Post = function(serverName, data, success, error) {
    $.ajax({
        url: DBHelper.host + 'api/' + serverName,
        beforeSend: function(request) {
            loadingImg()
            request.setRequestHeader("Authorization", 'bearer ' + localStorage.getItem("token"));
        },
        
        type: 'POST',
        data: data,	
        
        success: function(data) {
            $('.loading').remove()
            success(data)
        },
        error: function(data) {
            $('.loading').remove()
            if (data.status == "401") {
                alert('请重新登录')
                location.href = "/admin/login.html"
                //location.href='login.html'
            }
            error(data)
        }
    });
}

DBHelper.Postp = function(serverName, data, success, error) { 
    $.ajax({
        url: DBHelper.host + 'api/' + serverName,
        beforeSend: function(request) {
            loadingImg()
            request.setRequestHeader("Authorization", 'bearer ' + localStorage.getItem("token"));
        },
//      contentType:"application/json",
        type: 'POST',
        contentType: 'application/json',
        data: JSON.stringify(data),
        
        success: function(data) {
            $('.loading').remove()
            success(data)
        },
        error: function(data) {
            $('.loading').remove()
            if (data.status == "401") {
                alert('请重新登录')
                location.href = "/admin/login.html"
                //location.href='login.html'
            }
            error(data)
        }
    });
}

DBHelper.PostA = function(serverName, data, success, error) {
    $.ajax({
        url: DBHelper.host + 'api/' + serverName,
        beforeSend: function(request) {
            loadingImg()
            request.setRequestHeader("Authorization", 'bearer ' + localStorage.getItem("token"));
        },
        
        type: 'POST',
        data: data,	
        async:false,
        success: function(data) {
            $('.loading').remove()
            success(data)
        },
        error: function(data) {
            $('.loading').remove()
            if (data.status == "401") {
                alert('请重新登录')
                location.href = "/admin/login.html"
                //location.href='login.html'
            }
            error(data)
        }
    });
}
//PUTÇëÇó
DBHelper.Put = function(serverName, data, success, error) {
    $.ajax({
        url: DBHelper.host + 'api/' + serverName,
        beforeSend: function(request) {
            loadingImg()
            request.setRequestHeader("Authorization", 'bearer ' + localStorage.getItem("token"));
        },
        data: data,
        type: 'PUT',
        success: function(data) {
            $('.loading').remove()
            success(data)
        },
        error: function(data) {
            $('.loading').remove()
            if (data.status == "401") {
                alert('请重新登录')
                location.href = "/admin/login.html"
                //location.href='login.html'
            }
            error(data)
        }
    });
}
//DELETEÇëÇó
DBHelper.Delete = function(serverName, data, success, error) {
    $.ajax({
        url: DBHelper.host + 'api/' + serverName,
        beforeSend: function(request) {
            loadingImg()
            request.setRequestHeader("Authorization", 'bearer ' + localStorage.getItem("token"));
        },
        data: JSON.stringify(data),
        dataType:"json",
        type: 'DELETE',
        contentType:"application/json",
        // dataType: "jsonp", //指定服务器返回的数据类型
        // jsonpCallback: "showData", //指定回调函数名称
        success: function(data) {
            $('.loading').remove()
            success(data)
        },
        error: function(data) {
            $('.loading').remove()
            if (data.status == "401") {
                alert('请重新登录')
                location.href = "/admin/login.html"
                //location.href='login.html'
            }
            if(serverName.indexOf("Admin_Xn_Rank/")!=-1){
            	let r = confirm("请不要删除默认等级")
            	if(r==true){
            		location.href = window.location.href
            	}else{
            		location.href = window.location.href
            	}
//          	comfirm("请不要删除非默认等级",function(){
//				      			window.location.reload()
//				      		})
            }
            
            error(data)
            console.log("serverName",serverName)
        }
    });
}

DBHelper.DeleteSingle = function(serverName, data, success, error) {
    $.ajax({
        url: DBHelper.host + 'api/' + serverName,
        beforeSend: function(request) {
            loadingImg()
            request.setRequestHeader("Authorization", 'bearer ' + localStorage.getItem("token"));
        },
//      data: JSON.stringify(data),
//      dataType:"json",
        type: 'DELETE',
//      contentType:"application/json",
        // dataType: "jsonp", //指定服务器返回的数据类型
        // jsonpCallback: "showData", //指定回调函数名称
        success: function(data) {
            $('.loading').remove()
            success(data)
        },
        error: function(data) {
            $('.loading').remove()
            if (data.status == "401") {
                alert('请重新登录')
                location.href = "/admin/login.html"
                //location.href='login.html'
            }
            error(data)
        }
    });
}

function loadingImg() {
    if ($('.loading').length == 0) {
        // statement
        $('body').append('<div class="loading" style="z-index:10000;top:0;left:0;position:fixed;width:100%;height:100%;background:rgba(0,0,0,0)">\
                                <svg class="lds-spinner" width="129px"  height="129px" style="position:absolute;top:50%;left:50%;margin-top:-64px;transform:translateX(-50%);"  xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 100 100" preserveAspectRatio="xMidYMid" style="background: none;"><g transform="rotate(0 50 50)">\
  <rect x="47" y="24" rx="9.4" ry="4.8" width="6" height="12" fill="rgba(16.862745098039213%,43.92156862745101%,98.4313725490196%,0.491)">\
    <animate attributeName="opacity" values="1;0" keyTimes="0;1" dur="1s" begin="-0.9166666666666666s" repeatCount="indefinite"></animate>\
  </rect>\
</g><g transform="rotate(30 50 50)">\
  <rect x="47" y="24" rx="9.4" ry="4.8" width="6" height="12" fill="rgba(16.862745098039213%,43.92156862745101%,98.4313725490196%,0.491)">\
    <animate attributeName="opacity" values="1;0" keyTimes="0;1" dur="1s" begin="-0.8333333333333334s" repeatCount="indefinite"></animate>\
  </rect>\
</g><g transform="rotate(60 50 50)">\
  <rect x="47" y="24" rx="9.4" ry="4.8" width="6" height="12" fill="rgba(16.862745098039213%,43.92156862745101%,98.4313725490196%,0.491)">\
    <animate attributeName="opacity" values="1;0" keyTimes="0;1" dur="1s" begin="-0.75s" repeatCount="indefinite"></animate>\
  </rect>\
</g><g transform="rotate(90 50 50)">\
  <rect x="47" y="24" rx="9.4" ry="4.8" width="6" height="12" fill="rgba(16.862745098039213%,43.92156862745101%,98.4313725490196%,0.491)">\
    <animate attributeName="opacity" values="1;0" keyTimes="0;1" dur="1s" begin="-0.6666666666666666s" repeatCount="indefinite"></animate>\
  </rect>\
</g><g transform="rotate(120 50 50)">\
  <rect x="47" y="24" rx="9.4" ry="4.8" width="6" height="12" fill="rgba(16.862745098039213%,43.92156862745101%,98.4313725490196%,0.491)">\
    <animate attributeName="opacity" values="1;0" keyTimes="0;1" dur="1s" begin="-0.5833333333333334s" repeatCount="indefinite"></animate>\
  </rect>\
</g><g transform="rotate(150 50 50)">\
  <rect x="47" y="24" rx="9.4" ry="4.8" width="6" height="12" fill="rgba(16.862745098039213%,43.92156862745101%,98.4313725490196%,0.491)">\
    <animate attributeName="opacity" values="1;0" keyTimes="0;1" dur="1s" begin="-0.5s" repeatCount="indefinite"></animate>\
  </rect>\
</g><g transform="rotate(180 50 50)">\
  <rect x="47" y="24" rx="9.4" ry="4.8" width="6" height="12" fill="rgba(16.862745098039213%,43.92156862745101%,98.4313725490196%,0.491)">\
    <animate attributeName="opacity" values="1;0" keyTimes="0;1" dur="1s" begin="-0.4166666666666667s" repeatCount="indefinite"></animate>\
  </rect>\
</g><g transform="rotate(210 50 50)">\
  <rect x="47" y="24" rx="9.4" ry="4.8" width="6" height="12" fill="rgba(16.862745098039213%,43.92156862745101%,98.4313725490196%,0.491)">\
    <animate attributeName="opacity" values="1;0" keyTimes="0;1" dur="1s" begin="-0.3333333333333333s" repeatCount="indefinite"></animate>\
  </rect>\
</g><g transform="rotate(240 50 50)">\
  <rect x="47" y="24" rx="9.4" ry="4.8" width="6" height="12" fill="rgba(16.862745098039213%,43.92156862745101%,98.4313725490196%,0.491)">\
    <animate attributeName="opacity" values="1;0" keyTimes="0;1" dur="1s" begin="-0.25s" repeatCount="indefinite"></animate>\
  </rect>\
</g><g transform="rotate(270 50 50)">\
  <rect x="47" y="24" rx="9.4" ry="4.8" width="6" height="12" fill="rgba(16.862745098039213%,43.92156862745101%,98.4313725490196%,0.491)">\
    <animate attributeName="opacity" values="1;0" keyTimes="0;1" dur="1s" begin="-0.16666666666666666s" repeatCount="indefinite"></animate>\
  </rect>\
</g><g transform="rotate(300 50 50)">\
  <rect x="47" y="24" rx="9.4" ry="4.8" width="6" height="12" fill="rgba(16.862745098039213%,43.92156862745101%,98.4313725490196%,0.491)">\
    <animate attributeName="opacity" values="1;0" keyTimes="0;1" dur="1s" begin="-0.08333333333333333s" repeatCount="indefinite"></animate>\
  </rect>\
</g><g transform="rotate(330 50 50)">\
  <rect x="47" y="24" rx="9.4" ry="4.8" width="6" height="12" fill="rgba(16.862745098039213%,43.92156862745101%,98.4313725490196%,0.491)">\
    <animate attributeName="opacity" values="1;0" keyTimes="0;1" dur="1s" begin="0s" repeatCount="indefinite"></animate>\
  </rect>\
</g></svg>\
                            </div>')
        //
    }
}



function getNowFormatDate() {
            var date = new Date();
            var seperator1 = "-";
            var seperator2 = ":";
            var month = date.getMonth() + 1;
            var strDate = date.getDate();
            if (month >= 1 && month <= 9) {
                month = "0" + month;
            }
            if (strDate >= 0 && strDate <= 9) {
                strDate = "0" + strDate;
            }
            var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
                + " " + date.getHours() + seperator2 + date.getMinutes()
                + seperator2 + date.getSeconds();
            return currentdate;
        }

//图片上传
function updataImg(file,success){	
    var data = new FormData();
    data.append("file", file.files[0]);
    $.ajax({
      url:DBHelper.host+'api/ImgUpload',
      type: 'POST',
      dataType: 'JSON',
      data: data,
      cache: false,
      processData: false,
      contentType: false,
      beforeSend: function(request) {
          request.setRequestHeader("Authorization", 'bearer ' + DBHelper.getToken());
          loadingImg()
      },
      success:function (res) {
        $('.loading').remove()
        success(res)
      },
      error:function (res) {
        $('.loading').remove()
        console.log(res)
        alert('上传失败')
      }
    })

}

//数组传输
DBHelper.PostArr = function(serverName, data, success, error) {
    $.ajax({
        url: DBHelper.host + 'api/' + serverName,
        beforeSend: function(request) {
            loadingImg()
            request.setRequestHeader("Authorization", 'bearer ' + localStorage.getItem("token"));
        },
        data: JSON.stringify(data),
        // dataType:"json",
        type: 'POST',
        contentType:"application/json",
        success: function(data) {
            $('.loading').remove()
            success(data)
        },
        error: function(data) {
            $('.loading').remove()
            if (data.status == "401") {
                alert('请重新登录')
                location.href = "/admin/login.html"
                //location.href='login.html'
//              location.href = "/admin/dist/views/login.html"
            }
            error(data)
        }
    });
}
